package com.mark.flutterhybridandroid.messageChannel;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.view.FlutterView;

/**
 * @Desc EventChannelPlugin
 *       用于数据流（event streams）的通信，持续通信，通过长用于Native向Dart的通信，
 *       如：手机电量变化，网络连接变化，陀螺仪，传感器等；
 * @作者 Mark
 * @时间 2019-04-30
 * @EMail 2285581945@qq.com
 */
public class EventChannelPlugin implements StreamHandler {
    private EventChannel.EventSink eventSink;

    //初始化，实例化
    public static EventChannelPlugin registerWith(FlutterView flutterView) {
       EventChannelPlugin plugin = new EventChannelPlugin();
       new EventChannel(flutterView,"EventChannelPlugin").setStreamHandler(plugin);
       return plugin;
    }

    public void send(Object params){
        if (eventSink!=null){
            eventSink.success(params);
        }
    }

    @Override
    public void onListen(Object o, EventChannel.EventSink eventSink) {
        this.eventSink = eventSink;
    }

    @Override
    public void onCancel(Object o) {
        eventSink = null;
    }
}
